package tom.adminlte.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import tom.adminlte.model.SysUserAuthentication;

/**
 * spring-security 用户信息
 * @author ZHUFEIFEI
 */
@Slf4j
@Service
public class BaseUserDetailsService implements UserDetailsService{
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("username ==> {}", username);
        SysUserAuthentication user = null;
        if (username.startsWith("admin")) {
            user = new SysUserAuthentication();
            user.setId(1L);
            user.setUsername(username);
            user.setPassword("{noop}123456");
            user.setAuthorities(AuthorityUtils.createAuthorityList("admin_role", "me"));
            user.setAccountNonExpired(true);
            user.setAccountNonLocked(true);
            user.setCredentialsNonExpired(true);
            user.setEnabled(true);

            log.info("user ==> {}", user);
        }
        //user != null 则验证通过
        return user;
    }
}
